package com.hsteach.web.controller.visit;

import com.hsteach.common.annotation.Anonymous;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.R;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.framework.feign.client.FeignConfig;
import com.hsteach.framework.feign.client.xgk.dto.ret.GetSchoolPartTreeRet;
import com.hsteach.service.third.XgkService;
import com.hsteach.service.visit.VisitRecordService;
import com.hsteach.web.controller.visit.dto.VisitStatisticsDto;
import com.hsteach.web.controller.visit.vo.VisitStatisticsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 家访统计Controller
 *
 * @author fanxquer
 * @date 2023-07-21
 */
@Api(value = "家访-统计", tags = "家访-统计")
@RestController
@RequestMapping("/visit/statistics")
public class VisitStatisticsController extends BaseController
{
    @Autowired
    private VisitRecordService visitRecordService;

    @Autowired
    private XgkService xgkService;

    /**
     * 查询学段列表
     */
    @ApiOperation("查询统计筛选班级树")
    @GetMapping("/tree")
    public R<GetSchoolPartTreeRet> list()
    {
        GetSchoolPartTreeRet schoolPartTree = xgkService.getSchoolPartTreeAuth(SecurityUtils.getAuthInfo().getPersonId());
        return R.ok(schoolPartTree);
    }

    /**
     * 家访统计
     */
    @ApiOperation("家访统计")
    @PostMapping
    public R<VisitStatisticsVo> statistics(@RequestBody VisitStatisticsDto dto)
    {
        VisitStatisticsVo statistics = visitRecordService.statistics(dto);
        return R.ok(statistics);
    }

    /**
     * 统计一个学校的整体家纺信息
     * @return
     */
    @ApiOperation("整校家访统计")
    @PostMapping("/all")
    @Anonymous
    public R<VisitStatisticsVo> statisticsAll(HttpServletRequest request) {
        //拷贝请求头到Feign的调用请求
        String token = request.getHeader("token");
        String orgCode = request.getHeader("orgCode");
        HashMap<String, String> map = new HashMap<>(2);
        map.put("token", token);
        map.put("orgCode", orgCode);
        FeignConfig.contact.set(map);
        VisitStatisticsVo statistics = visitRecordService.statisticsAllBySchoolCode(orgCode);
        statistics.setClasses(null);
        return R.ok(statistics);
    }
}
